import request from '@/utils/request'

// 分页查询试卷列表
export function getPageData(searchModel) {
  return request({
    url: '/paper/selectPageData',
    method: 'post',
    data: {
      page: searchModel.currentPage,
      size: searchModel.pageSize,
      title: searchModel.title,
      status: searchModel.status
    }
  })
}

// 保存试卷
export function save(data) {
  return request({
    url: '/paper/save',
    method: 'post',
    data
  })
}

// 获取试卷详情
export function getDetail(paperId) {
  return request({
    url: `/paper/detail/${paperId}`,
    method: 'get'
  })
}

// 发布试卷
export function publish(paperId) {
  return request({
    url: `/paper/publish/${paperId}`,
    method: 'post'
  })
}

// 归档试卷
export function archive(paperId) {
  return request({
    url: `/paper/archive/${paperId}`,
    method: 'post'
  })
}

// 删除试卷
export function deletePaper(paperId) {
  return request({
    url: `/paper/delete/${paperId}`,
    method: 'delete'
  })
}

// 批量删除试卷
export function batchDelete(paperIds) {
  return request({
    url: '/paper/deleteBatch',
    method: 'post',
    data: paperIds
  })
}

// 向试卷添加题目
export function addQuestion(paperId, questionId, score) {
  return request({
    url: '/paper/addQuestion',
    method: 'post',
    params: {
      paperId,
      questionId,
      score
    }
  })
}

// 批量向试卷添加题目
export function addQuestions(paperId, questionIds) {
  return request({
    url: '/paper/addQuestions',
    method: 'post',
    params: {
      paperId
    },
    data: questionIds
  })
}

// 从试卷移除题目
export function removeQuestion(paperId, questionId) {
  return request({
    url: '/paper/removeQuestion',
    method: 'delete',
    params: {
      paperId,
      questionId
    }
  })
}

// 自动组卷
export function autoCompose(paperId, categoryId, difficultyLevel, questionCount) {
  return request({
    url: '/paper/autoCompose',
    method: 'post',
    params: {
      paperId,
      categoryId,
      difficultyLevel,
      questionCount
    }
  })
}

// 调整题目顺序
export function adjustOrder(paperId, orderList) {
  return request({
    url: '/paper/adjustOrder',
    method: 'post',
    params: {
      paperId
    },
    data: orderList
  })
}

// 获取已发布的试卷列表
export function getPublishedPapers() {
  return request({
    url: '/paper/published',
    method: 'get'
  })
}

export default {
  getPageData,
  save,
  getDetail,
  publish,
  archive,
  delete: deletePaper,
  batchDelete,
  addQuestion,
  addQuestions,
  removeQuestion,
  autoCompose,
  adjustOrder,
  getPublishedPapers
}

// 为了兼容，添加一些别名
export { getDetail as getPaperDetail }
export { addQuestions as addQuestionsToPaper }
export { removeQuestion as removeQuestionFromPaper }
export { autoCompose as autoComposePaper }
